package cn.stylefeng.guns.sys.modular.system.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.modular.system.entity.BtyXd;
import cn.stylefeng.guns.sys.modular.system.entity.BtyXdgt;
import cn.stylefeng.guns.sys.modular.system.service.BtyXdService;
import cn.stylefeng.guns.sys.modular.system.service.BtyXdgtService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *巡店沟通控制器
 * @author liuy
 * @Date
 */
@Controller
@RequestMapping("/btyXdgt")
public class BtyXdgtController extends BaseController {

    private String PREFIX = "/modular/system/btyXdgt/";
    @Resource
    private BtyXdService btyXdService;
    private String userName="admin";
    @Resource
    private BtyXdgtService btyXdgtService;
    /**
     *是否是督导的标志off
     */
    private String off="off";
    /**
     * 跳转到通知列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyXdgt.html";
    }
    /**
     * 获取通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,String dm) {
        //获取用户的帐号
        Long userId= LoginContextHolder.getContext().getUserId();
        String username=LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        String roleName="巡店管理员";
        Page<Map<String, Object>> list;
        if(userName.equals(username)|roleNames.contains(roleName)){
            list = this.btyXdgtService.list(condition,null);
        }else {
            list = this.btyXdgtService.list(condition,userId);
        }
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }
    /**
     * 获取巡店沟通导出excel列表
     */
    @RequestMapping(value = "/listExport")
    @ResponseBody
    public Object listExport(String condition) {
        //获取用户的帐号
        Long userId= LoginContextHolder.getContext().getUserId();
        String username=LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        String roleName="巡店管理员";
        List<Map<String, Object>> list;
        if(userName.equals(username)|roleNames.contains(roleName)){
            list = this.btyXdgtService.listExport(condition,null);
        }else {
            list = this.btyXdgtService.listExport(condition,userId);
        }

        return list;
    }
    /**
     * 跳转到添加档案
     */
    @RequestMapping("/btyXdgt_add")
    public String noticeAdd() {
        return PREFIX + "btyXdgt_add.html";
    }
    /**
     * 跳转到修改档案
     */
    @RequestMapping("/btyXdgt_update/{id}")
    public String noticeUpdate(@PathVariable Long id, Model model) {
        BtyXdgt btyXd = this.btyXdgtService.getById(id);
        model.addAllAttributes(BeanUtil.beanToMap(btyXd));
        LogObjectHolder.me().set(btyXd);
        return PREFIX + "btyXdgt_edit.html";
    }
    /**
     * 修改档案
     * @author liuy
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BtyXdgt btyXd,@RequestParam String sumXd) {
        if (ToolUtil.isOneEmpty(btyXd,btyXd.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        btyXd.setSumXd(sumXd);
        this.btyXdgtService.updateById(btyXd);
        return SUCCESS_TIP;
    }

    /**
     * 新增报销档案
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(BtyXdgt btyXd,@RequestParam String sumXd) {
        if (ToolUtil.isOneEmpty(btyXd)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        btyXd.setSumXd(sumXd);
        btyXd.setCreateUser(LoginContextHolder.getContext().getUserId());
        btyXd.setCreateTime(new Date());
        this.btyXdgtService.save(btyXd);
        return SUCCESS_TIP;
    }
    /**
     * 档案详情
     */
    @RequestMapping("/info")
    @ResponseBody
    public Object getThemeInfo(@RequestParam String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new RequestEmptyException();
        }
        BtyXdgt btyXd=this.btyXdgtService.getById(id);
        return btyXd;
    }
    /**
     * 删除报销档案
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long id) {
        this.btyXdgtService.removeById(id);
        return SUCCESS_TIP;
    }
   /**
     * 查询店名
     */
    @RequestMapping(value = "/getDm")
    @ResponseBody
    public List<BtyXd> getDm(){
        String username=LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        String roleName="巡店管理员";
        List<BtyXd> list;
        if(userName.equals(username)|roleNames.contains(roleName)){
            list=btyXdgtService.dmList(null);
        }else{
            list=btyXdgtService.dmList(username);
            if(list.size()==0){
                list=btyXdService.dmList(username);
            }
        }

        return list;
    }

    /**
     * 获取上次的巡店信息
     */
    @RequestMapping(value = "/lastInfo")
    @ResponseBody
    public BtyXdgt lastInfo(){
        Long userId= LoginContextHolder.getContext().getUserId();
        BtyXdgt btyXdgt=btyXdgtService.lastInfo(userId);
        return btyXdgt;
    }

}
